﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using MySql.Data.MySqlClient;
using System.Text;

namespace DA
{
    public class Login : GO.BaseClass
    {
        public VO.Login objVO;

        public Login()
        {
            objVO = new VO.Login();
            sbSQL = new StringBuilder();
        }

        public void Salvar()
        {
            // MONTA A INSTRUÇÃO SQL
            sbSQL.Append(" INSERT INTO Login (Usuario, Senha, Email, DataNascimento) ");
            sbSQL.Append(" VALUES('p_Usuario','p_Senha','p_Email','p_DataNascimento'); ");

            // SUBSTITUI OS PARÂMETROS
            sbSQL.Replace("p_Usuario", objVO.Usuario);
            sbSQL.Replace("p_Senha", objVO.Senha);
            sbSQL.Replace("p_Email", objVO.Email);
            sbSQL.Replace("p_DataNascimento", objVO.DataNascimento.ToString("s"));

            // EXECUTA INSTRUÇÃO
            GO.MySQLHelper.ExecuteNonQuery(getConn(), sbSQL.ToString(), CommandType.Text);

            // LIMPA VARIÁVEL
            sbSQL.Remove(0, sbSQL.Length);
        }

        public void Atualizar()
        {
            // MONTA A INSTRUÇÃO SQL
            sbSQL.Append(" UPDATE `Login` SET");
            sbSQL.Append("     Usuario  = 'p_Usuario ',");
            sbSQL.Append("     Senha  = 'p_Senha ',");
            sbSQL.Append("     Email  = 'p_Email ', ");
            sbSQL.Append("     DataNascimento = 'p_DataNascimento' ");
            sbSQL.Append(" WHERE ");
            sbSQL.Append(" 	   idLogin   = p_idLogin  ;");

            // SUBSTITUI OS PARÂMETROS
            sbSQL.Replace("p_idLogin", objVO.IdLogin.ToString());
            sbSQL.Replace("p_Usuario", objVO.Usuario);
            sbSQL.Replace("p_Senha", objVO.Senha);
            sbSQL.Replace("p_Email", objVO.Email);
            sbSQL.Replace("p_DataNascimento", objVO.DataNascimento.ToString("s"));

            GO.MySQLHelper.ExecuteNonQuery(getConn(), sbSQL.ToString(), CommandType.Text);

            sbSQL.Remove(0, sbSQL.Length);
        }

        public void Excluir()
        {
            // Deleta 
            sbSQL.Append(" DELETE FROM `Login` ");
            sbSQL.Append(" WHERE idLogin   = p_idLogin  ; ");

            // SUBSTITUI OS PARÂMETROS
            sbSQL.Replace("p_idLogin", objVO.IdLogin.ToString());

            GO.MySQLHelper.ExecuteNonQuery(getConn(), sbSQL.ToString(), CommandType.Text);
        }

        public DataSet getTodosRegistros()
        {

            sbSQL.Append(" SELECT * FROM `Login`; ");

            return GO.MySQLHelper.ExecuteDataset(getConn(), sbSQL.ToString(), CommandType.Text);
        }

        public DataSet getRegistroById()
        {
            sbSQL.Append(" SELECT * FROM `Login` ");
            sbSQL.Append(" WHERE idLogin = p_idLogin; ");
            sbSQL.Replace("p_idLogin", objVO.IdLogin.ToString());

            return GO.MySQLHelper.ExecuteDataset(getConn(), sbSQL.ToString(), CommandType.Text);
        }

        public DataSet getLogin()
        {
            sbSQL.Append(" SELECT * FROM `Login` ");
            sbSQL.Append(" WHERE Usuario = 'p_Usuario' AND Senha = 'p_Senha'; ");
            sbSQL.Replace("p_Usuario", objVO.Usuario.ToString());
            sbSQL.Replace("p_Senha", objVO.Senha.ToString());

            return GO.MySQLHelper.ExecuteDataset(getConn(), sbSQL.ToString(), CommandType.Text);

        }


    }
}
